package product;

import java.util.*;
import java.util.Date;

public class Show {
    private int id;
    private Movie movie;
    private String hall;
    private Date time;
    private double price;
    private int rowCount, colCount;
    private final Set<Seat> soldSeats = new HashSet<>();

    public Show(int id, Movie movie, String hall, Date time, double price, int rowCount, int colCount) {
        this.id = id;
        this.movie = movie;
        this.hall = hall;
        this.time = time;
        this.price = price;
        this.rowCount = rowCount;
        this.colCount = colCount;
    }

    public boolean checkSeatAvailability(int row, int col) {
        return row >= 1 && row <= rowCount && col >= 1 && col <= colCount
                && !soldSeats.contains(new Seat(row, col));
    }

    public void occupySeats(Set<Seat> seats) {
        soldSeats.addAll(seats);
    }

    public void releaseSeat(Seat seat) {
        soldSeats.remove(seat);
    }

    public void displaySeats() {
        for (int i = 1; i <= rowCount; i++) {
            for (int j = 1; j <= colCount; j++)
                System.out.print(soldSeats.contains(new Seat(i, j)) ? "X " : "O ");
            System.out.println();
        }
    }

    public String seatsToString() {
        StringBuilder sb = new StringBuilder();
        for (Seat s : soldSeats)
            sb.append(s.getRow()).append(",").append(s.getCol()).append(";");
        return sb.toString();
    }

    public void stringToSeats(String str) {
        soldSeats.clear();
        if (str == null || str.isEmpty()) return;
        for (String s : str.split(";")) {
            if (s.isEmpty()) continue;
            String[] p = s.split(",");
            if (p.length == 2)
                soldSeats.add(new Seat(Integer.parseInt(p[0]), Integer.parseInt(p[1])));
        }
    }

    // Getters and Setters
    public int getId() { return id; }
    public Movie getMovie() { return movie; }
    public String getHall() { return hall; }
    public Date getTime() { return time; }
    public double getPrice() { return price; }
    public int getRowCount() { return rowCount; }
    public int getColCount() { return colCount; }
    public Set<Seat> getSoldSeats() { return new HashSet<>(soldSeats); }
    public void setHall(String hall) { this.hall = hall; }
    public void setTime(Date time) { this.time = time; }
    public void setPrice(double price) { this.price = price; }
}